<%@ page contentType="text/html;charset=UTF-8"%>
<%@page  import="java.util.List"%>
<%@page  import="java.util.Calendar"%>
<%@page import="net.sf.json.JSONArray"%>
<%@ page import="com.apache.workday.*" %>
<%@ page import="com.apache.tools.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<%
String init = request.getParameter("init");
if(StrUtil.isNotNull(init)){
   //WorkDayData.getInstance().startInstance();
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
	<script type="text/javascript" src="${ctx}/js/easyui/jquery-1.8.0.min.js"></script>
	<script type="text/javascript" src="${ctx}/js/WdatePicker/WdatePicker.js"></script>
	<script type="text/javascript" src='${ctx}/js/zbx-workday.js'></script>
	<link rel="stylesheet" type="text/css" href="${ctx}/css/zbx-workday.css">
	
	<script type="text/javascript">
	//初始化日期
	function initCal(year){
		document.write("<table>");		
		for(var i=1;i<=12;i++){
			if ((i - 1) % 4 == 0){document.write("<tr>");}
			document.write("<td>");
			workDay(year , i);
			document.write("</td>");
			if (i % 4 == 0){document.write("</tr>");}			
		}
		document.write("</table>");
	}

	/**点击日期，进行提交设置*/
	 function clickDateNode(node){
		var setWorkday = "F";//默认设置休息日
		if(node.className.indexOf('calendar-selected')>-1){
			setWorkday = "T";//如果已经是休息日，则说明是要设置为工作日
		}
		$.post("workday-do.jsp", {act:"set", day: node.id, isWorkday: setWorkday },
  		 function(data){
     		if(setWorkday == "F"){
				node.className +=' calendar-selected ';
			}else{
				node.className = node.className.replace('calendar-selected','');
			}
   		});
	}	
	
	//根据已经设置的工作日进行反显
	function displayCss(jsonAry){
		for(var i=0;i<jsonAry.length;i++){
			var jsonDay = jsonAry[i];
			//alert(jsonDay.isWorkday);
			setDayCss(jsonDay.dateStr,jsonDay.isWorkday);
		}	
	}

	//计算n天工作日后的日期
	function afterNWorkday(){
		var startDay  =$('#startDay').val();
		var ndays  = $.trim($('#ndays').val());
		//alert(startDay);
		if(startDay.length<1){
			alert("起始日期不能为空!"); 
			$('#startDay').focus();
			return ;
		}
		if(ndays.length<1){
			alert("工作日数不能为空!"); 
			$('#ndays').focus();
			return ;
		}
		
		if(!/^\d+$/.test(ndays)){
			alert("工作日数必须为数字！"); 
			$('#ndays').focus();
			return ;
		}
		
		$.post("workday-do.jsp", {act:"after", day:startDay, ndays: ndays },
	  		 function(data){
	  		 	document.getElementById('testResult').value = data;
	  		 }
	  	 );
	}
	
	function clearDay(){
		$("#startDay").val("");
		$("#ndays").val("");
		$("#testResult").val("");
	}
	
</script>
</head>
	
	<%
		String year = request.getParameter("year");
		if(year==null){
			year = new String(String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
		}
		
		java.util.List<Workday> list =  WorkdayUtils.getInstance().getDayListOfYear(year);
		String jsonStr = JSONArray.fromObject(list).toString();
	%>

	<script type="text/javascript">
	   function initWrokday(){
		   location = "workday2.jsp?init=init";
	   }
	</script>
<input type="button" value="初始化日历" onclick="initWrokday()"><br>
	
	<body>
	<form action="workday.jsp" method="post">
		请选择年份<select name="year">
		<% int y = Calendar.getInstance().get(Calendar.YEAR);
			for(int i =y-2;i<=y+2;i++){ %>
		<option value="<%=i %>" <%if(year.equals(""+i)){out.print(" selected ");} %>><%=i %></option>
		<%} %>
		</select>
		<input type="submit" value="查询">
	</form>

		
		<script type="text/javascript">
			initCal('<%=year%>');			
			var jsonAry = jQuery.parseJSON('<%=jsonStr%>');
			displayCss(jsonAry);
		</script>

	</body>
</html>



